<template>
	<el-container>
		<el-aside width="260px" v-loading="showTreeLoading">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键词过滤" v-model="treeFilterText" clearable></el-input>
					<el-checkbox v-model="treeExpand" @change="handleTreeExpand($event)" style="margin-left: 10px;">展开</el-checkbox>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="tree" class="menu" node-key="id" :data="treeData" :props="treeProps" :current-node-key="''"
							 :highlight-current="true" :expand-on-click-node="false" default-expand-all
							 :filter-node-method="treeFilterNode" @node-click="treeClick"></el-tree>
				</el-main>
			</el-container>
		</el-aside>
		<el-container>
			<el-header>
				<div class="left-panel">
					<el-button v-auth="'manage:user:add'" type="primary" icon="el-icon-plus" @click="add">新增</el-button>
					<el-button v-auth="'manage:user:remove'" type="danger" plain icon="el-icon-delete" :disabled="selection.length===0" @click="batchDel">删除</el-button>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.keyword" placeholder="用户名/昵称/手机号" clearable></el-input>
						<sc-select v-model="search.status" dict="common_status" placeholder="请选择状态" style="width: 100%;" clearable></sc-select>
						<el-button v-auth="'manage:user:page'" type="primary" icon="el-icon-search" @click="upSearch">搜索</el-button>
						<el-button @click="resetSearch">重置</el-button>
					</div>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="dataList" @selection-change="selectionChange" stripe remoteSort remoteFilter>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label="序号" type="index"></el-table-column>
					<el-table-column label="用户名" prop="username" align="center" width="120"></el-table-column>
					<el-table-column label="头像" align="center" width="100">
						<template #default="scope">
							<el-avatar :src="scope.row.avatar" size="small"></el-avatar>
						</template>
					</el-table-column>
					<el-table-column label="所属部门" prop="dept.deptName" align="center" width="180"></el-table-column>
					<el-table-column label="状态" prop="status" align="center" width="100">
						<template #default="scope">
							<el-switch v-model="scope.row.status" @change="changeStatus($event, scope.row)" :loading="scope.row.$switch_status" active-value="1" inactive-value="0"></el-switch>
						</template>
					</el-table-column>
					<el-table-column label="昵称" prop="nickname" align="center" width="120"></el-table-column>
					<el-table-column label="手机号" prop="mobile" align="center" width="150"></el-table-column>
					<el-table-column label="所属角色" prop="roleList" align="center" width="150">
						<template #default="scope">
							<div v-for="role in scope.row.roleList" :key="role.id">
								<el-tag>{{ role.roleName }}</el-tag>
							</div>
						</template>
					</el-table-column>
					<el-table-column label="姓名" prop="realName" align="center" width="120"></el-table-column>
					<el-table-column label="性别" prop="gender" align="center" width="100">
						<template #default="scope">
							<p v-if="scope.row.gender==='1'">男</p>
							<p v-if="scope.row.gender==='0'">女</p>
						</template>
					</el-table-column>
					<el-table-column label="生日" prop="birthDate" align="center" width="150"></el-table-column>
					<el-table-column label="身份证号" prop="idCard" align="center" width="180"></el-table-column>
					<el-table-column label="创建时间" prop="createTime" align="center" width="150" sortable='custom'></el-table-column>
					<el-table-column label="操作" fixed="right" align="center" width="200">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small" @click="tableShow(scope.row, scope.$index)">
									查看
								</el-button>
								<el-button v-auth="'manage:user:edit'" text type="primary" size="small" @click="tableEdit(scope.row, scope.$index)">
									编辑
								</el-button>
								<el-popconfirm title="确定重置吗？" @confirm="tableResetPwd(scope.row, scope.$index)">
									<template #reference>
										<el-button v-auth="'manage:user:resetPwd'" text type="primary" size="small">重置密码</el-button>
									</template>
								</el-popconfirm>
								<el-popconfirm title="确定删除吗？" @confirm="tableDel(scope.row, scope.$index)">
									<template #reference>
										<el-button v-auth="'manage:user:remove'" text type="primary" size="small">删除</el-button>
									</template>
								</el-popconfirm>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="refreshTable" @closed="dialog.save=false"></save-dialog>

</template>

<script>
import saveDialog from './save.vue'

export default {
	name: 'user',
	components: {
		saveDialog
	},
	data() {
		return {
			dialog: {
				save: false
			},
			selection: [],
			search: {
				keyword: null,
				status: null
			},
			dataList: this.$API.manage.user.page,
			treeExpand: true,
			treeFilterText: null,
			showTreeLoading: false,
			treeData: [],
			treeProps: {
				label: "deptName",
				children: "children"
			}
		}
	},
	watch: {
		treeFilterText(val) {
			this.$refs.tree.filter(val);
		}
	},
	mounted() {
		this.getTreeData();
	},
	methods: {
		// 加载树数据
		async getTreeData() {
			this.showTreeLoading = true;
			const data = await this.$API.manage.dept.list.get();
			this.showTreeLoading = false;
			this.treeData = data;
		},
		// 树权限（展开/折叠）
		handleTreeExpand(value) {
			let treeList = this.treeData;
			for (let i = 0; i < treeList.length; i++) {
				this.$refs.tree.store.nodesMap[treeList[i].id].expanded = value;
			}
		},
		// 树过滤
		treeFilterNode(value, data) {
			if (!value) return true;
			return data.deptName.indexOf(value) !== -1;
		},
		// 树点击
		treeClick(data) {
			const params = {deptId: data.id};
			this.$refs.table.reload(params)
		},
		// 添加
		add() {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open();
			})
		},
		// 批量删除
		batchDel() {
			this.$confirm(`确定删除选中的 ${this.selection.length} 项数据吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				const loading = this.$loading();
				const idList = [];
				this.selection.forEach(e => idList.push(e.id));
				const reqData = {idList: idList};
				this.$API.manage.user.remove.delete(reqData).then(() => {
					this.$refs.table.refresh();
					this.$message.success("删除成功");
				})
				loading.close();
			})
		},
		// 查看
		tableShow(row) {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row);
			})
		},
		// 编辑
		tableEdit(row) {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row);
			})
		},
		// 重置密码
		tableResetPwd(row) {
			const reqData = {idList: [row.id]};
			this.$API.manage.user.resetPwd.put(reqData).then(() => {
				this.$refs.table.refresh();
				this.$message.success("密码重置成功");
			})
		},
		// 删除
		tableDel(row) {
			const reqData = {idList: [row.id]};
			this.$API.manage.user.remove.delete(reqData).then(() => {
				this.$refs.table.refresh();
				this.$message.success("删除成功");
			})
		},
		// 修改状态
		changeStatus(val, row) {
			row.$switch_status = true;
			const reqData = {id: row.id, status: row.status};
			this.$API.manage.user.changeStatus.put(reqData).then(() => {
				delete row.$switch_status;
				this.$refs.table.refresh();
				this.$message.success("操作成功");
			})
		},
		// 搜索
		upSearch() {
			this.$refs.table.upData(this.search);
		},
		// 重置
		resetSearch() {
			this.search.keyword = null;
			this.search.status = null;
			this.$refs.table.upData(this.search);
		},
		// 表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		// 列表刷新
		refreshTable() {
			this.$refs.table.refresh();
		}
	}
}
</script>

<style>
</style>
